package com.example.bpmn.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.task.Task;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.HashMap;
import com.example.bpmn.domain.DeliveryStatus;

@Slf4j
@RestController
@RequestMapping("/api/delivery")
@RequiredArgsConstructor
public class DeliveryController {

    private final TaskService taskService;

    @GetMapping("/tasks")
    public List<Map<String, Object>> getTasks() {
        return taskService.createTaskQuery()
            .processDefinitionKey("deliveryProcess")
            .active()
            .list()
            .stream()
            .map(this::convertTaskToMap)
            .collect(Collectors.toList());
    }

    @GetMapping("/tasks/{processInstanceId}")
    public List<Map<String, Object>> getTasksByProcessInstance(@PathVariable String processInstanceId) {
        return taskService.createTaskQuery()
            .processInstanceId(processInstanceId)
            .active()
            .list()
            .stream()
            .map(this::convertTaskToMap)
            .collect(Collectors.toList());
    }

    private Map<String, Object> convertTaskToMap(Task task) {
        Map<String, Object> result = new HashMap<>();
        result.put("id", task.getId());
        result.put("name", task.getName());
        result.put("processInstanceId", task.getProcessInstanceId());
        result.put("taskDefinitionKey", task.getTaskDefinitionKey());
        result.put("createTime", task.getCreateTime());
        result.put("assignee", task.getAssignee());
        return result;
    }

    @PostMapping("/tasks/manual-routing/{taskId}/complete")
    public void completeManualRouting(@PathVariable String taskId) {
        log.info("完成手动排线任务: taskId={}", taskId);
        
        // 获取流程变量
        Task task = taskService.createTaskQuery()
            .taskId(taskId)
            .singleResult();
        String processInstanceId = task.getProcessInstanceId();
        
        // 更新运单状态
        Map<String, Object> variables = new HashMap<>();
        variables.put("deliveryStatus", DeliveryStatus.WAIT_DISPATCH);
        
        // 完成任务
        taskService.complete(taskId, variables);
    }

    @PostMapping("/tasks/arrange-driver/{taskId}/complete")
    public void completeArrangeDriver(@PathVariable String taskId) {
        log.info("完成安排司机任务: taskId={}", taskId);
        
        Map<String, Object> variables = new HashMap<>();
        variables.put("deliveryStatus", DeliveryStatus.DISPATCHED);
        taskService.complete(taskId, variables);
    }

    @PostMapping("/tasks/checkpoint/{taskId}/complete")
    public void completeCheckpoint(@PathVariable String taskId) {
        log.info("完成站点打卡任务: taskId={}", taskId);
        taskService.complete(taskId);
    }

    @PostMapping("/tasks/central-dispatch/{taskId}/complete")
    public void completeCentralDispatch(@PathVariable String taskId) {
        log.info("完成中心仓派车任务: taskId={}", taskId);
        taskService.complete(taskId);
    }

    @PostMapping("/tasks/complete-waybill/{taskId}/complete")
    public void completeWaybillInfo(@PathVariable String taskId) {
        log.info("完成运单信息任务: taskId={}", taskId);
        taskService.complete(taskId);
    }
} 